package com.itheima.web.job;

import cn.hutool.core.collection.CollUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.service.mongo.SoundService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class JobManager {
    @Autowired
    private FastFileStorageClient client;
    @Autowired
    private FdfsWebServer webServer;

    @Reference
    private SoundService soundService;

    public void soundGC() {//传音删除过期
        long halfYear =1000*3600*24*30*6;
        //查询半年前的传音
        long halfYearAgo = System.currentTimeMillis() - halfYear;
        List<String> surls = soundService.findOldSoundUrl(halfYearAgo);
        if (CollUtil.isNotEmpty(surls)) {
            for (String sUrl : surls) {
                String[] split = sUrl.split(":");
                String url = split[2].substring(5);//取出相对路径
                client.deleteFile(url);//删除
            }
        }

    }
}
